home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / haeberli / pstools / pstile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  5.0 KB  |  233 lines

  1. /*
  2.  * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18.  *    pstile -
  19.  *        Repeat copies of a page across the printed sheet
  20.  
  21.  *    To compile:
  22.  *        cc pstile.c -o pstile 
  23.  *
  24.  *                Paul Haeberli - 1993
  25.  */
  26. #include "stdio.h"
  27. #include "math.h"
  28. #include "ctype.h"
  29.  
  30. #define SWIDTH          (72.0*8.50)     /* sheet width and height */
  31. #define SHEIGHT         (72.0*11.0)
  32.  
  33. #define INBUFSIZE    4096
  34. #define MAXPAGES    1000
  35.  
  36. char buf[INBUFSIZE];
  37. int secstart[MAXPAGES];
  38. int secend[MAXPAGES];
  39. int trailstart;
  40. int trailend;
  41. float pdx, pdy, pscale;
  42. float pwidth, pheight;
  43.  
  44. indexpages(inf)
  45. FILE *inf;
  46. {
  47.     int i, n, before, after;
  48.  
  49.     n = 0;
  50.     trailstart = 0;
  51.     trailend = 0;
  52.     while(fgets(buf,INBUFSIZE,inf)) {
  53.     if(strncmp(buf,"%%Page:",7) == 0) {
  54.         after = ftell(inf);
  55.         if(n>0)
  56.         secend[n-1] = before;
  57.         secstart[n] = after; 
  58.         n++;
  59.     } else if(strncmp(buf,"%%Trailer",9) == 0) {
  60.         after = ftell(inf);
  61.         if(n>0)
  62.         secend[n-1] = before;
  63.         trailstart = after;
  64.         trailend = sizeoffile(inf);
  65.         return n;
  66.     }
  67.     before = ftell(inf);
  68.     }
  69.     secend[n-1] = ftell(inf);
  70.     return n;
  71. }
  72.  
  73. putpage(inf,pageno,npages)
  74. FILE *inf;
  75. int pageno, npages;
  76. {
  77.     int nbytes;
  78.  
  79.     if(pageno<npages && pageno>=0) {
  80.     nbytes = secend[pageno]-secstart[pageno];
  81.     putsection(inf,secstart[pageno],nbytes);
  82.     }
  83. }
  84.  
  85. putsection(inf,start,nbytes)
  86. FILE *inf;
  87. int start, nbytes;
  88. {
  89.     int nr;
  90.  
  91.     fseek(inf,start,0);
  92.     while(nbytes>0) {
  93.     if(!fgets(buf,INBUFSIZE,inf)) {
  94.         fprintf(stderr,"psbook: bad read in putsection\n");
  95.         exit(1);
  96.     }
  97.     nr = strlen(buf);
  98.     nbytes -= nr;
  99.     if(buf[0] != '%')
  100.         fputs(buf,stdout);
  101.     }
  102.     if(nbytes != 0) {
  103.     fprintf(stderr,"psbook: strange read in putsection\n");
  104.     exit(1);
  105.     }
  106. }
  107.  
  108. printheader(dosettrans)
  109. int dosettrans;
  110. {
  111.     printf("%%!PS-Adobe-2.0\n");
  112.     printf("%%%%EndComments\n");
  113.     printf("/REALshowpage /showpage load def\n");
  114.     printf("/showpage { } def\n");
  115.     if(dosettrans)
  116.     psprintcortab();
  117. }
  118.  
  119. main(argc,argv)
  120. int argc;
  121. char **argv;
  122. {
  123.     FILE *inf;
  124.     int npages, dorot, i, dosettrans;
  125.  
  126.     if(argc<2) {
  127.      fprintf(stderr,"\nusage: pstile in.ps [-s pagedx pagedy] [-r] [-nosettrans] > out.ps\n");
  128.      exit(1);
  129.     }
  130.     pwidth = 8.5;
  131.     pheight = 11.0;
  132.     dorot = 0;
  133.     dosettrans = 1;
  134.     for(i=2; i<argc; i++) {
  135.         if(argv[i][0] == '-') {
  136.             switch(argv[i][1]) {
  137.                 case 'r':
  138.             dorot = 1;
  139.                     break;
  140.                 case 's':
  141.                     i++;
  142.                     pwidth = atof(argv[i]);
  143.                     i++;
  144.                     pheight = atof(argv[i]);
  145.                     break;
  146.         case 'n':
  147.                     dosettrans = 0;
  148.                     break;
  149.         }
  150.     }
  151.     }
  152.     pwidth *= 72.0;
  153.     pheight *= 72.0;
  154.     psoutfile(stdout);
  155.     printheader(dosettrans);
  156.     inf = fopen(argv[1],"r");
  157.     if(!inf) {
  158.      fprintf(stderr,"psbook: can't open %s\n",argv[1]);
  159.      exit(1);
  160.     }
  161.     npages = indexpages(inf);
  162.     fprintf(stderr,"Document pages %d\n",npages);
  163.     if(npages == 0) {
  164.     fprintf(stderr,"psfold: Document does not have page info\n");
  165.     exit(1);
  166.     } 
  167.     putpages(inf,npages,dorot);
  168.     fclose(inf);
  169.     exit(0);
  170. }
  171.  
  172. putpages(inf,npages,dorot)
  173. FILE *inf;
  174. int npages, dorot;
  175. {
  176.     int pageno, x, y, nx, ny;
  177.     float cx, cy, dx, dy;
  178.     
  179.     printf("%%%%Page: page%d %d\n",1,1);
  180.     printf("showpage\n");
  181.     if(dorot) {
  182.     dy = pwidth;
  183.     dx = pheight;
  184.     } else {
  185.     dx = pwidth;
  186.     dy = pheight;
  187.     }
  188.     nx = SWIDTH/dx;
  189.     ny = SHEIGHT/dy;
  190.     if(nx<1) nx = 1;
  191.     if(ny<1) ny = 1;
  192.     cx = (SWIDTH-nx*dx)/2.0;
  193.     cy = (SHEIGHT-ny*dy)/2.0;
  194.     for(pageno=0; pageno<npages; pageno++) {
  195.     printf("%%%%Page: page%d %d\n",pageno+2,pageno+2);
  196.     for(y=0; y<ny; y++) {
  197.         for(x=0; x<nx; x++) {
  198.         dosave(inf);
  199.         if(dorot) {
  200.             printf("%f %f translate\n",cx+x*dx+pheight,cy+y*dy);
  201.             printf("90 rotate\n");
  202.         } else {
  203.             printf("%f %f translate\n",cx+x*dx,cy+y*dy);
  204.         }
  205.         printf("%f %f moveto\n",0.0,0.0);
  206.         printf("%f %f lineto\n",pwidth,0.0);
  207.         printf("%f %f lineto\n",pwidth,pheight);
  208.         printf("%f %f lineto\n",0.0,pheight);
  209.         printf("closepath clip newpath\n");
  210.         putsection(inf,0,secstart[0]);
  211.         putpage(inf,pageno,npages);
  212.         dorestore(inf);
  213.         }
  214.     }
  215.     }
  216.     printf("REALshowpage\n");
  217.     printf("%%%%EOF\n");
  218. }
  219.  
  220. dosave(inf)
  221. FILE *inf;
  222. {
  223.     printf("gsave\n");
  224. }
  225.  
  226. dorestore(inf)
  227. FILE *inf;
  228. {
  229.     if(trailstart) 
  230.     putsection(inf,trailstart,trailend-trailstart);
  231.     printf("grestore\n"); 
  232. }
  233.